package com.meitutest.autoscript;

import android.os.RemoteException;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiWatcher;

import com.meitutest.autoscript.utils.TestReport;

import java.util.regex.Pattern;

/**
 * Created by CrystalChen on 2017/12/18.
 */

public class MyWatcher implements UiWatcher {
    UiDevice mDevice;

    public MyWatcher(UiDevice mDevice) {
        this.mDevice = mDevice;
    }

    @Override
    public boolean checkForCondition() {
        if (mDevice.hasObject(By.text("老化测试警告"))) {
            mDevice.pressBack();
            TestReport.i("MyWatcher_checkForCondition: 发现老化警告弹窗");
            return true;
        } else if (mDevice.hasObject(By.text("OK"))) {
            mDevice.findObject(By.text("OK")).click();
            TestReport.i("MyWatcher_checkForCondition: 发现OK弹窗，点击同意");
            return true;
        } else if (mDevice.hasObject(By.text("ALLOW"))) {
            mDevice.findObject(By.text("ALLOW")).click();
            TestReport.i("MyWatcher_checkForCondition: 发现ALLOW弹窗，点击同意");
            return true;
        } else if (mDevice.hasObject(By.text("同意"))) {
            mDevice.findObject(By.text("同意")).click();
            TestReport.i("MyWatcher_checkForCondition: 发现弹窗，点击同意");
            return true;
        } else if (mDevice.hasObject(By.text(Pattern.compile("确定|OK|知道了|同意")))) {
            mDevice.findObject(By.text(Pattern.compile("确定|OK|知道了|同意"))).click();
            TestReport.i("MyWatcher_checkForCondition: 发现弹窗，点击 确定|OK|知道了|同意");
            return true;
        } else if (mDevice.hasObject(By.text("以后再说"))) {
            mDevice.findObject(By.text("以后再说")).click();
            TestReport.i("MyWatcher_checkForCondition: 发现弹窗-以后再说，点击以后再说");
            return true;
        }else if (mDevice.hasObject(By.text("给我们评分"))) {
            mDevice.findObject(By.text("不了，谢谢")).click();
            TestReport.i("MyWatcher_checkForCondition: 发现弹窗-给我们评分，点击不了，谢谢");
            return true;
        }else try {
            if (!mDevice.isScreenOn()) {
                mDevice.pressHome();
                TestReport.i("MyWatcher_checkForCondition: 发现灭屏，点击HOME亮屏");
                return true;
            } else if (mDevice.hasObject(By.res("com.meitu.mobile.meitukeyguard", "meitu_minute_second1"))) {
                mDevice.swipe(570, 1650, 570, 400, 100);
                TestReport.i("MyWatcher_checkForCondition: 发现锁屏，点击确定");
                return true;
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return false;
    }
}
